/*
 * ShoppingCart.java
 *
 */

package vendingMachine.gui;

/**
 *
 * @author Mike
 */
import java.util.Iterator;
import java.util.ArrayList;

import vendingMachine.inventory.Item;

public class ShoppingCart
{
    
    ArrayList itemList;
    
    /** Creates a new instance of ShoppingCart */
    public ShoppingCart ()
    {
        itemList = new  ArrayList ();
    }
    
    public void addItem (Item item)
    {
        ShoppingCartItem scItem = this.getItem (item.getID ());
        
        if (scItem == null)
        {
            scItem = new ShoppingCartItem (item);
            itemList.add (scItem);
        }
        
        scItem.incrementQuantity ();
        
    }
    
    public void removeItem (int productID)
    {
        ShoppingCartItem scItem = this.getItem (productID);
        if (scItem != null)
        {
            scItem.decrementQuantity ();
            if (scItem.getQuantity () == 0)
                itemList.remove (scItem);
        }
    }
    
    public ShoppingCartItem getItem (int productID)
    {
        Iterator iterator = this.createIterator ();
        while (iterator.hasNext ())
        {
            ShoppingCartItem scItem = (ShoppingCartItem)iterator.next ();
            if (scItem.getProductID () == (productID))
                return scItem;
        }
        return null;
    }
    
    public int getNumberOfItems ()
    {
        return itemList.size ();
    }
    
    public double calculateTotal ()
    {
        double totalCost;
        Iterator iterator = this.createIterator ();
        
        totalCost = 0;
        while (iterator.hasNext ())
        {
            ShoppingCartItem scItem = (ShoppingCartItem)iterator.next ();
            totalCost = scItem.getCost () + totalCost;
        }
        return totalCost;
    }
    
    public ArrayList getItemList ()
    {
        return itemList;
    }
    
    public Iterator createIterator ()
    {
        return new ItemListIterator (itemList);
    }
    
}
